package com.gxa.service.impl;

import com.gxa.domain.entity.StockHistory;
import com.gxa.domain.vo.StockHistoryVO;
import com.gxa.mapper.StockHistoryMapper;
import com.gxa.service.StockHistoryService;
import com.gxa.common.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StockHistoryServiceImpl implements StockHistoryService {
    
    @Autowired
    private StockHistoryMapper stockHistoryMapper;
    
    @Override
    public int insert(StockHistory stockHistory) {
        return stockHistoryMapper.insert(stockHistory);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchInsert(List<StockHistory> stockHistoryList) {
        if (stockHistoryList == null || stockHistoryList.isEmpty()) {
            return 0;
        }
        
        try {
            return stockHistoryMapper.batchInsert(stockHistoryList);
        } catch (Exception e) {
            // 记录详细错误信息
            System.err.println("批量插入上货历史记录失败: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("批量插入上货历史记录失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public Page selectPage(Map<String, Object> params) {
        // 计算分页参数
        Integer currPage = 1;
        Integer pageSize = 10;
        
        // 安全地获取并转换参数
        Object currentObj = params.get("current");
        Object sizeObj = params.get("size");
        
        if (currentObj != null) {
            if (currentObj instanceof Integer) {
                currPage = (Integer) currentObj;
            } else if (currentObj instanceof String) {
                try {
                    currPage = Integer.parseInt((String) currentObj);
                } catch (NumberFormatException e) {
                    currPage = 1;
                }
            }
        }
        
        if (sizeObj != null) {
            if (sizeObj instanceof Integer) {
                pageSize = (Integer) sizeObj;
            } else if (sizeObj instanceof String) {
                try {
                    pageSize = Integer.parseInt((String) sizeObj);
                } catch (NumberFormatException e) {
                    pageSize = 10;
                }
            }
        }
        
        // 确保参数在有效范围内
        if (currPage < 1) {
            currPage = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        
        int offset = (currPage - 1) * pageSize;
        params.put("offset", offset);
        params.put("limit", pageSize);
        
        // 查询数据（返回VO对象）
        List<StockHistoryVO> list = stockHistoryMapper.selectPage(params);
        long totalCount = stockHistoryMapper.selectCount(params);
        
        // 构建分页对象
        Page page = new Page(list, totalCount, pageSize, currPage);
        
        return page;
    }
    
    @Override
    public List<StockHistoryVO> selectByBarcodeNo(String barcodeNo) {
        return stockHistoryMapper.selectByBarcodeNo(barcodeNo);
    }
    
    @Override
    public List<StockHistoryVO> selectByOperatorId(Integer operatorId) {
        return stockHistoryMapper.selectByOperatorId(operatorId);
    }
    
    @Override
    public List<StockHistoryVO> selectByTimeRange(String startTime, String endTime) {
        return stockHistoryMapper.selectByTimeRange(startTime, endTime);
    }

    @Override
    public int deleteById(Integer id) {
        return stockHistoryMapper.deleteById(id);
    }

    @Override
    public int batchDelete(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return 0;
        }
        return stockHistoryMapper.batchDelete(ids);
    }
} 